{block name="content"}
<form class="layui-form layui-card" action="{:sysuri()}" data-auto="false" method="post" autocomplete="off" lay-filter="stock_form" id="stock_form" onsubmit="return false;">
    <div class="layui-card-body padding-left-40">
        <div class="layui-form-item">
            <table id="sku_stock" lay-filter="sku_stock"></table>
        </div>
        <div class="layui-form-item text-center">
            <div class="layui-input-block">
                <input type="hidden" name="goods_id" value="{$goods_id|default=0}">
                <button class="layui-btn" type='button' lay-submit lay-filter="stock_submit">保存数据</button>
            </div>
        </div>
    </div>
</form>
{/block}

{block name="script"}
<script>
    layui.use(['table','form'],function(){
        let table = layui.table
        let form = layui.form
        let form_data = form.val('stock_form')
        // 渲染，并获得实例对象
        let inst = table.render({
            elem:'#sku_stock',even:true,id:'sku_stock',toolbar:true,url:'{:sysuri("getSkuStock")}'
            ,method:'post',where:form_data,
            cols: [[
                {type: 'checkbox', width:60, fixed: 'left'},
                {field:'goods_nu',title:'{:lang("商品SKU")}',minWidth:80,align:'center'},
                {field:'attribute_titles',title:'{:lang("商品规格")}',minWidth:100,align:'center'},
                {field:'stock',title:'{:lang("库存")}',minWidth:300,align:'left', edit: 'text'},
            ]],
            parseData: function(res){ // res 即为原始返回的数据
                return {
                    "code": res.code, // 解析接口状态
                    "msg": res.info, // 解析提示文本
                    "count": res.data.total, // 解析数据长度
                    "data": res.data.item // 解析数据列表
                };
            },
        });
        form.on('submit(stock_submit)', function(data){
            let table_data = table.getData('sku_stock')
            if(table_data.length <= 0){
                $.msg.error('sku库存无数据');
                return false;
            }
            let err_flag = false
            layui.each(table_data, function (index, item) {
                if(!isNotEmpty(item.stock) || (item.stock < 0)){
                    $.msg.error(item.goods_nu+'库存数据有误');
                    err_flag = true
                    return true;
                }
            });
            if(err_flag){
                return false;
            }
            httpRequest('{:sysuri("setInventory")}', {sku_data:JSON.stringify(table_data),goods_id:form_data.goods_id}, 'post', function(info, data){
                $.msg.success(info, 3, function(){
                    $.msg.closeThisModal('#stock_form')
                })
            })
            return false;
        })
    })
</script>
{/block}